<?php

namespace App\Models;

use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use phpDocumentor\Reflection\Types\Self_;

class ProductsBatchModel extends BaseModel
{
    use SoftDeletes;
    use SoftCascadeTrait;

    protected $softCascade = ['Quality', 'ProductSale', 'ProductLoss', 'WarehouseSiteRelation'];

    protected $fillable    = ['batch_id', 'unit', 'number', 'quality_type', 'quality_result', 'auth_type'];

    protected $primaryKey  = 'batch_id';

    protected $table       = 'products_batch';

    const MAP_UNIT    = [1 => '吨', 2 => '千克', 3 => '克', 4 => '斤'];
    const MAP_SOURCE  = [1 => '自产', 2 => '收购'];
    const MAP_AUTH    = [0 => '无', 1 => '无公害农产品', 2 => '有机食品', 3 => '绿色食品', 4 => '农产品地理标识'];
    const MAP_QUALITY = [1 => '自检', 2 => '委托检测', 3 => '抽检'];

    protected $appends = ['stock'];

    public function setAuthTypeAttribute($value)
    {
        if ($value == []) {
            return $this->attributes['auth_type'] = 0;
        }
        return $this->attributes['auth_type'] = implode(',', $value);
    }

    public function getStockAttribute()
    {
        return $this['number'] - $this['loss_number'] - $this['sale_number'];
    }

    public function getImageAttribute($value)
    {
        if ($value) {
            !is_array($value) && $value = json_decode($value, true);
            $items = [];
            foreach ($value as $i) {
                !is_array($i) && $i = json_decode($i, true);
                $items[] = $i;
            }
            return $items;
        } else {
            return [];
        }
    }

    public function getQualityImageAttribute($value)
    {
        if ($value) {
            !is_array($value) && $value = json_decode($value, true);
            $items = [];
            foreach ($value as $i) {
                !is_array($i) && $i = json_decode($i, true);
                $items[] = $i;
            }
            return $items;
        } else {
            return [];
        }
    }

    public function getVideoAttribute($value)
    {
        if ($value) {
            !is_array($value) && $value = json_decode($value, true);
            $items = [];
            foreach ($value as $i) {
                !is_array($i) && $i = json_decode($i, true);
                $items[] = $i;
            }
            return $items;
        } else {
            return [];
        }
    }

    public function Product()
    {
        return $this->belongsTo(ProductModel::class, 'product_id', 'product_id')->withTrashed();
    }

    public function ProductLoss()
    {
        return $this->hasMany(ProductsBatchLossModel::class, 'batch_id', 'batch_id');
    }

    public function ProductSale()
    {
        return $this->hasMany(ProductsSaleModel::class, 'batch_id', 'batch_id')->withTrashed();
    }

    public function WarehouseSite()
    {
        return $this->belongsToMany(WarehouseSiteModel::class, 'warehouse_site_batch', 'batch_id', 'site_id')->withTimestamps();
    }

    public function WarehouseSiteRelation()
    {
        return $this->hasMany(WarehouseRelationModel::class, 'batch_id', 'batch_id');
    }

    public function Customer()
    {
        return $this->belongsTo(CustomerModel::class, 'customer_id', 'customer_id')->withTrashed();
    }

    public function Quality()
    {
        return $this->hasOne(ProductBatchQualityModel::class, 'quality_result_id', 'quality_result_id');
    }

    public function Base()
    {
        return $this->hasOne(BasesModel::class, 'base_id', 'base_id');
    }
}
